package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.BaseService;
import com.cn.wanxi.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    /**
     * 1.判断验证码
     * 2.判断用户名和密码是否正确
     *
     * @param model
     * @return
     */
    @Override
    public ResultModel login(UserModel model) {
        ResultModel resultModel = new ResultModel();
        if (!model.getSessionCode().equals(model.getCode())) {
//        } else {
            resultModel.setMsg("error-code");
            return resultModel;
        }
        //        req.setAttribute("key", getName());
//        需要讲数据转换为json对象
//        JSONObject jsonObject=new JSONObject();
//        jsonObject.put("username",username);
//        jsonObject.put("pa",password);
//        jsonObject.put("model",model);
//            resultModel.setMsg("success");
//            只有验证码正确，才判断是否需要查询用户名和密码
//            查询数据库（dao）
        UserDao userDao = new UserImpl();
        UserModel userModel = userDao.findByUsernameAndPassword(model);
//            int a = 0;
//            int b = 2;
//            int c = 0;
//            if (a > b) {
//                c = 5;
//            } else {
//                c = 6;
//            }
//            int c = a > b ? 5 : 6;//三元运算符：三目运算符
//            ==等于  ！=不等于
//            sql  ！=   <>
        boolean isHave = userModel != null;//应该是根据用户名和密码判断是否有数据
        if (!isHave) {
//            resultModel.setMsg("success");
//        } else {
            resultModel.setMsg("error");
            return resultModel;
        }
        resultModel.setMsg("success");
        return resultModel;
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        List<UserModel> list = userDao.findAll(userModel);
        resultModel.setData(list);
        return resultModel;
    }

    @Override
    public ResultModel add(UserModel model) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.add(model);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel findById(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        UserModel model = userDao.findById(userModel);
        resultModel.setData(model);
        return resultModel;
    }

    @Override
    public ResultModel update(UserModel model) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.update(model);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel delete(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.delete(userModel);
        if (count > 0) {
            resultModel.setMsg("success");
        } else {
            resultModel.setMsg("error");
        }
        return resultModel;
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        ResultModel resultModel = new ResultModel();
        UserDao userDao = new UserImpl();
        int count = userDao.getCount(userModel);
        resultModel.setCount(count);
        return resultModel;
    }

//    private String aa() {
//        if (!true) {
//
////        } else {
//            return "";
//        }
//        if (!true) {
//
////        } else {
//            return "";
//        }
//        return "";
//    }
//    private void bb(){
//        if (true) {
//            return "";
//        } else {
//            return "";
//        }
//    }
}
